#include "kernel_operator.h"
#include <type_traits>
constexpr int32_t BUFFER_NUM = 2;
template<typename TYPE_X, typename TYPE_Y> class KernelAsinh {
    using T = TYPE_X;
public:
    __aicore__ inline KernelAsinh() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, uint32_t smallCoreDataNum,
                                uint32_t bigCoreDataNum, uint32_t finalBigTileNum, 
                                uint32_t finalSmallTileNum, uint32_t tileDataNum, 
                                uint32_t smallTailDataNum, uint32_t bigTailDataNum, 
                                uint32_t tailBlockNum) 
    {
        ASSERT(AscendC::GetBlockNum() != 0 && "block dim can not be zero!");
        uint32_t coreNum = AscendC::GetBlockIdx();
        uint32_t globalBufferIndex = bigCoreDataNum * AscendC::GetBlockIdx();
        this->tileDataNum = tileDataNum;
        if (coreNum < tailBlockNum) { 
          this->coreDataNum = bigCoreDataNum;
          this->tileNum = finalBigTileNum;
          this->tailDataNum = bigTailDataNum;
        }
        else { 
          this->coreDataNum = smallCoreDataNum;
          this->tileNum = finalSmallTileNum;
          this->tailDataNum = smallTailDataNum;
          globalBufferIndex -= (bigCoreDataNum - smallCoreDataNum) * (AscendC::GetBlockIdx() - tailBlockNum);
        }
        xGm.SetGlobalBuffer((__gm__ TYPE_X*)x + globalBufferIndex, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ TYPE_Y*)y + globalBufferIndex, this->coreDataNum);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_X));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(TYPE_Y));
        
        // 所有类型都需要
        
        if constexpr (std::is_same_v<T, half>) {
          pipe.InitBuffer(tmp1, this->tileDataNum * sizeof(float));
          pipe.InitBuffer(tmp2, this->tileDataNum * sizeof(float));
          pipe.InitBuffer(tmp3, this->tileDataNum * sizeof(float));
        } else if constexpr (std::is_same_v<T, float>) {
          pipe.InitBuffer(tmp1, this->tileDataNum * sizeof(float));
          pipe.InitBuffer(tmp2, this->tileDataNum * sizeof(float));
          pipe.InitBuffer(tmp3, this->tileDataNum * sizeof(float));
        }
        
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_X> xLocal = inQueueX.AllocTensor<TYPE_X>();
      AscendC::DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
      inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
      AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>();
      AscendC::LocalTensor<TYPE_Y> yLocal = outQueueY.AllocTensor<TYPE_Y>();
      
     if constexpr(std::is_same_v<T, half>){
        AscendC::LocalTensor<float> x_local_fp32 = tmp1.Get<float>();
        AscendC::LocalTensor<float> tmpFloatTensor2 = tmp2.Get<float>();
        AscendC::LocalTensor<float> y_local_fp32 = tmp3.Get<float>();
        
        // 转成float进行计算以实现更多的数据存储
        AscendC::Cast(x_local_fp32, xLocal, AscendC::RoundMode::CAST_NONE, this->processDataNum);
        AscendC::Mul(tmpFloatTensor2, x_local_fp32, x_local_fp32, this->processDataNum);
        AscendC::Adds(tmpFloatTensor2, tmpFloatTensor2, (float)1.0, this->processDataNum);
        AscendC::Sqrt(tmpFloatTensor2, tmpFloatTensor2, this->processDataNum);
        AscendC::Sub(tmpFloatTensor2, tmpFloatTensor2, x_local_fp32, this->processDataNum);
        AscendC::Ln(tmpFloatTensor2, tmpFloatTensor2, this->processDataNum);
        AscendC::Duplicate(x_local_fp32, (float)0.0, this->processDataNum);
        AscendC::Sub(y_local_fp32, x_local_fp32, tmpFloatTensor2, this->processDataNum);
        AscendC::Cast(yLocal, y_local_fp32, AscendC::RoundMode::CAST_NONE, this->processDataNum);
      }
      else if constexpr(std::is_same_v<T, float>){
        AscendC::LocalTensor<T> tmpTensor1 = tmp1.Get<T>();
        AscendC::LocalTensor<T> tmpTensor2 = tmp2.Get<T>();
        AscendC::LocalTensor<T> tmpTensor3 = tmp3.Get<T>(); 
        // 使用-ln(sqrt(x^2 + 1) - x) 来计算可以更好的避免因为正数过大造成尾数丢失的问题
        // 可能要用到更高维度的信息来存储平方的结果避免精度丢失 或者使用其他的操作代替平方计算方式
        AscendC::Mul(tmpTensor1, xLocal, xLocal, this->processDataNum);
        AscendC::Adds(tmpTensor1, tmpTensor1, (T)1.0, this->processDataNum);
        AscendC::Sqrt(tmpTensor2, tmpTensor1, this->processDataNum);
        AscendC::Sub(tmpTensor2, tmpTensor2, xLocal, this->processDataNum);
        AscendC::Ln(tmpTensor2, tmpTensor2, this->processDataNum);
        AscendC::Duplicate(tmpTensor3, (T)0.0, this->processDataNum);
        AscendC::Sub(yLocal, tmpTensor3, tmpTensor2, this->processDataNum);
      }
      outQueueY.EnQue<TYPE_Y>(yLocal);
      inQueueX.FreeTensor(xLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
      AscendC::LocalTensor<TYPE_Y> yLocal = outQueueY.DeQue<TYPE_Y>();  
      AscendC::DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
      outQueueY.FreeTensor(yLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::QuePosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmp1, tmp2, tmp3;
    AscendC::GlobalTensor<TYPE_X> xGm;
    AscendC::GlobalTensor<TYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

extern "C" __global__ __aicore__ void asinh(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
  GET_TILING_DATA(tiling_data, tiling);
  KernelAsinh<DTYPE_X, DTYPE_Y> op;
  op.Init(x, y, tiling_data.smallCoreDataNum, 
            tiling_data.bigCoreDataNum, tiling_data.finalBigTileNum, 
            tiling_data.finalSmallTileNum, tiling_data.tileDataNum, 
            tiling_data.smallTailDataNum, tiling_data.bigTailDataNum, 
            tiling_data.tailBlockNum);
  op.Process();
}

